Tensorflow多层感知机Eager API
1 | from __future__ import print_function |
设置 Eager API
1 | # Set Eager API |
导入数据集
1 | # Import MNIST data |
Extracting ./data/train-images-idx3-ubyte.gz
Extracting ./data/train-labels-idx1-ubyte.gz
Extracting ./data/t10k-images-idx3-ubyte.gz
Extracting ./data/t10k-labels-idx1-ubyte.gz
定义参数
1 | # Parameters |
数据拆分成批
1 | # Using TF Dataset to split data into batches |
定义多层感知机模型
1 | # Define the neural network. To use eager API and tf.layers API together, |
定义损失函数+优化方法+准确率
1 | # Cross-Entropy loss function |
训练
1 | # Training |
Initial loss= 2.362281322
Step: 0001 loss= 2.362281322 accuracy= 0.0391
Step: 0100 loss= 0.583163500 accuracy= 0.8291
Step: 0200 loss= 0.247603565 accuracy= 0.9281
Step: 0300 loss= 0.214451462 accuracy= 0.9360
Step: 0400 loss= 0.182251021 accuracy= 0.9452
Step: 0500 loss= 0.139149994 accuracy= 0.9585
Step: 0600 loss= 0.120601922 accuracy= 0.9649
Step: 0700 loss= 0.114957660 accuracy= 0.9655
Step: 0800 loss= 0.111238368 accuracy= 0.9660
Step: 0900 loss= 0.085549861 accuracy= 0.9754
Step: 1000 loss= 0.079464689 accuracy= 0.9752
测试评估
1 | # Evaluate model on the test image set |
Testset Accuracy: 0.9707